<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/8/1
 * Time: 上午9:16
 */

namespace app\index\controller;


use app\common\service\BaiduApiService;
use app\common\service\LogInfoService;
use app\common\service\MeetingService;
use app\common\service\SignService;
use app\common\service\ThirdPartService;
use think\Session;

class Thirdpaid extends Base
{
    public $private_key = '@fdwqe!231~';
    //发送验证码接口
    public function verifyCode(){
        $code=rand(1000,9999);
        $phone=input('post.phone');
        $data = array(
            "code" =>(string)$code,
            "product" => '发送的验证码'
        );
        Session::set('v_code',$code);//Se['v_code']=$code;
        $result=ThirdPartService::S()->smsSend($phone,$data);
        //发送了短信要记录数据库
        if($result){
            $this->ajax(200,'success');
        }else{
            $this->ajax(202,'fail');
        }
    }
    //人脸报名接口
    public function signUp(){
        $sessionVCode=Session::get('v_code');
        $v_code=input('post.v_code');
        if($v_code!=$sessionVCode){
           // $this->ajax('202','验证码错误');
        }
        $add['phone']=input('post.telephone');
        $add['token']=input('post.token');//会议token
        $add['name']=input('post.card_name');
        $add['idcard']=input('post.idcard');
        $idcardimg=input('post.idcardimg');
        $headimg=input('post.headimg');
        LogInfoService::S()->writeLog('log','signUp',$_POST);
        //查询出会议信息
        $meetingInfo=MeetingService::S()->queryMeeting(['token'=>$add['token']]);
        if(!$meetingInfo){
            $this->ajax(202,'会议token错误');
        }
        $add['meeting_id']=$meetingInfo['id'];
        //查询是否重复报名
        $sign=SignService::S()->querySign(['idcard'=>$add['idcard'],'meeting_id'=>$meetingInfo['id']]);
        if($sign&&!empty($sign)){
            $this->ajax(202,'您已经报名了本次会议不能重复报名哦');
        }
        if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $headimg, $result)) {
            $headimg=str_replace($result[1], '',$headimg);
            $type = $result[2];
            if (in_array($type, array('pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png'))) {
                $new_file = $add['name']."_head_". date('YmdHis_') . '.' . $type;
                if (file_put_contents($new_file,base64_decode($headimg))) {
                    //$img_path = str_replace('../../..', '', $new_file);
                    $re=ThirdPartService::S()->uploadpicByPath($new_file,$type);
                    if($re['code']==200){
                        ThirdPartService::S()->unlink(ROOT_PATH."public/".$new_file);
                        $add['headimg']=$re['data'];
                    }
                } else {
                    $this->ajax(202,'头像上传错误');
                }
            } else {
                //文件类型错误
                $this->ajax(202,'图片上传类型错误');
            }
        }

        //处理身份证图片
        if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $idcardimg, $result)) {
            $idcardimg=str_replace($result[1], '', $idcardimg);
            $type = $result[2];
            if (in_array($type, array('pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png'))) {
                $new_file = $add['name']."_idcard_". date('YmdHis_') . '.' . $type;
                if (file_put_contents($new_file, base64_decode($idcardimg))) {
                    //$img_path = str_replace('../../..', '', $new_file);
                    $re=ThirdPartService::S()->uploadpicByPath($new_file,$type);
                    if($re['code']==200){
                        ThirdPartService::S()->unlink(ROOT_PATH."public/".$new_file);//删除上传的图片
                        $add['idcardimg']=$re['data'];
                    }
                } else {
                    $this->ajax(202,'身份证上传错误');
                }
            } else {
                //文件类型错误
                $this->ajax(202,'身份证上传类型错误');
            }
        }
        $add['appiyid']=$add['token'];
        unset($add['token']);
        $id=SignService::S()->addSign($add);
        if($id){
            //百度报名接口
            $reApply=BaiduApiService::S()->faceApply($id,$add['name'],$add['token'],$add['phone'],$headimg);
            if(isset($reApply['error_code'])){//百度返回失败
                LogInfoService::S()->writeLog('ThirdPaid','signUp-apply_error',$reApply);
            }
            //成功后生成秘钥
            $msg = xn_encrypt(strval($id), $this->private_key);
            $this->ajax(200,'success',$msg);
            $content=array(
                'name'=>$add['name'],
                'meeting'=>':'.$meetingInfo['meeting_name'],
                'qrcode'=>':waoo77676'//$reApply['result']['qrcode']
            );
            $tempCode='SMS_141205900';
            //发送短信
            $send=ThirdPartService::S()->smsSend($add['phone'],$content,$tempCode);
            $this->ajax(200,'success');
        }else{
            $this->ajax(202,'error');
        }
    }


    //签到反馈
    public function feedBack(){
        //echo md5('waoo2018');
        $log_id=input('post.log_id');
        $uProjectId=input('post.uProjectId');
        $userInfo=input('post.user');
        LogInfoService::S()->writeLog('ThirdPaid','feedBack',$_POST);
        $userInfo=base64_decode($userInfo);
        //查询出会议token
        $meetingInfo=MeetingService::S()->queryMeeting(['id'=>$uProjectId]);
        $userInfo=decrypt($userInfo,$meetingInfo['token']);
        //修改用户签到状态
        $userInfo=json_decode($userInfo,true);
        $re=SignService::S()->saveSign($userInfo['applyId']);
        if($re){
            echo 'success';
        }else{
            echo 'error';
        }
    }

    //验证接收app发送的信息 并返回签到结果
    public function getVerifyByApp(){
        $code=input('get.code');
        if($code!=md5('waoo2018')){
            LogInfoService::S()->writeLog('ThirdPaid','getVerifyByApp','验证错误:'.$code);
           echo 'error';exit();
        }
        $data=input('http://input');
        $data=json_decode($data,true);
        $return=BaiduApiService::S()->verifyFace($data);
        echo json($return);exit();
    }

    public function test(){
        $content=array(
            'name'=>'刘洪',
            'meeting'=>'-参加会议',
            'url'=>'123456789'
        );
        $tempCode='SMS_141580107';
        //发送短信
        //$accessKeyId=$config['accessKeyId'];//'LTAIf2zRmxxWeODT';
        //$accessKeySecret=$config['accessKeySecret'];//'K6cmDfbi4Mddrm6yKTbm8xp1PNN5dK'
        $config=array(
            'accessKeyId'=>'LTAIIY2wdcpXslTl',
            'accessKeySecret'=>'9T6Bd91YN9kAZbY8Z9WYaXP4W4F2Xq'
        );
        $send=ThirdPartService::S()->smsSend(17316364297,$content,$tempCode,$config,'金江海');
        dump($send);die;
    }

}